package com.bigger.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigger.constant.ErrorConstant;
import com.bigger.context.BaseContext;
import com.bigger.dto.ShoppingCarDTO;
import com.bigger.entity.BusinessMP;
import com.bigger.entity.ShoppingCar;
import com.bigger.exception.BaseException;
import com.bigger.mapper.ShoppingCarMapper;
import com.bigger.service.BusinessService;
import com.bigger.service.ShoppingCarService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private BusinessService businessService;
    @Override
    public List<ShoppingCar> get() {
        QueryWrapper<ShoppingCar> warpper=new QueryWrapper<ShoppingCar>()
                .eq("user_id", BaseContext.getCurrentId());
        List<ShoppingCar> shoppingCars = shoppingCarMapper.selectList(warpper);
        return shoppingCars;
    }

    @Override
    public void add(ShoppingCarDTO shoppingCarDTO) {
        if (StringUtils.isBlank(shoppingCarDTO.getClaimCode())){
            throw new BaseException(ErrorConstant.CLAIM_CODE_IS_EMPTY);
        }
        if (shoppingCarDTO.getNumber()<=0||shoppingCarDTO.getNumber()>100){
            throw new BaseException(ErrorConstant.NUMBER_IS_ERROR);
        }
        QueryWrapper<ShoppingCar> warpper=new QueryWrapper<ShoppingCar>()
                .eq("user_id",BaseContext.getCurrentId())
                .eq("business_id",shoppingCarDTO.getBusinessId());
        ShoppingCar shoppingCar = shoppingCarMapper.selectOne(warpper);
        if (shoppingCar!=null){
            int number=shoppingCar.getNumber()+shoppingCarDTO.getNumber();
            if(number>100){
                throw new BaseException(ErrorConstant.NUMBER_IS_ERROR);
            }
            shoppingCar.setNumber(number);
            shoppingCar.setClaimCode(shoppingCar.getClaimCode()+"，"+shoppingCarDTO.getClaimCode());
            shoppingCarMapper.updateById(shoppingCar);
        }else{
            BusinessMP business = businessService.getById(shoppingCarDTO.getBusinessId());
            shoppingCar=new ShoppingCar(
                    null,shoppingCarDTO.getBusinessId(),BaseContext.getCurrentId(),business.getName(),business.getPrice(),shoppingCarDTO.getNumber(), shoppingCarDTO.getClaimCode()
            );
            shoppingCarMapper.insert(shoppingCar);
        }
    }

    @Override
    public void update(ShoppingCarDTO shoppingCarDTO) {
        if (shoppingCarDTO.getClaimCode().isEmpty()){
            throw new BaseException(ErrorConstant.CLAIM_CODE_IS_EMPTY);
        }
        if (shoppingCarDTO.getNumber()<=0||shoppingCarDTO.getNumber()>100){
            throw new BaseException(ErrorConstant.NUMBER_IS_ERROR);
        }
        ShoppingCar shoppingCar = shoppingCarMapper.selectById(shoppingCarDTO.getId());
        shoppingCar.setNumber(shoppingCarDTO.getNumber());
        shoppingCar.setClaimCode(shoppingCarDTO.getClaimCode());
        shoppingCarMapper.updateById(shoppingCar);
    }

    @Override
    public void delete(Long id) {
        shoppingCarMapper.deleteById(id);
    }

    @Override
    public ShoppingCar getById(Long id) {
        ShoppingCar shoppingCar = shoppingCarMapper.selectById(id);
        return shoppingCar;
    }

    @Override
    public void deleteAll() {
        QueryWrapper<ShoppingCar> wrapper=new QueryWrapper<ShoppingCar>()
                .eq("user_id",BaseContext.getCurrentId());
        shoppingCarMapper.delete(wrapper);
    }
}
